// promise 的实现
function myPromise(executor) {
    let self = this;
    this.status = "pending";
    this.value = undefined;
    this.reason = undefined;
    this.onResovedCallbacks = [];
    this.onRejectedCallbacks = [];
    function resolve(value) {
        if (self.status == "pending") {
            self.status = "resolved";
            self.value = value;
            self.onResovedCallbacks.forEach(fn => fn());
        }
    }
    function reject(error) {
        if (self.status == "pending") {
            self.status = "rejected";
            self.reason = error;
            self.onRejectedCallbacks.forEach(fn => fn())
        }
    }
    try {
        executor(resolve, reject)
    } catch (error) {
        reject(error)
    }
}

myPromise.prototype.then = function (infulfilled, inrejected) {
    let self = this;
    let promise2;
    infulfilled = typeof infulfilled == "function" ? infulfilled : function (value) {
        return value
    }
    inrejected = typeof inrejected == "function" ? inrejected : function (err) {
        throw err
    }

    if (this.status == "pending") {
        promise2 = new myPromise(function (resolve, reject) {
            self.onResovedCallbacks.push(function () {
                setTimeout(function () {
                    try {
                        let x = infulfilled(self.value)
                        resolvePromise(promise2, x, resolve, reject)
                    } catch (error) {
                        reject(error)
                    }
                })
            });
            self.onRejectedCallbacks.push(function () {
                setTimeout(function () {
                    try {
                        let x = inrejected(self.reason)
                        resolvePromise(promise2, x, resolve, reject)
                    } catch (error) {
                        reject(error);
                    }
                })
            })
        })
    }

    if (this.status == "resolved") {
        promise2 = new myPromise(function (resolve, reject) {
            setTimeout(function () {
                try {
                    let x = infulfilled(self.value);
                    resolvePromise(promise2, x, resolve, reject)
                } catch (error) {
                    reject(error)
                }
            })
        })
    }
    if (this.status == "rejected") {
        promise2 = new myPromise(function (resolve, reject) {
            setTimeout(function () {
                try {
                    let x = inrejected(self.value);
                    resolvePromise(promise2, x, resolve, reject)
                } catch (error) {
                    reject(error)
                }
            })
        })
    }
    return promise2
}

function resolvePromise(p2, x, resolve, reject) {
    if (p2 == x || x !== undefined) {
        reject(new TypeError("类型错误"))
    }
    if (x !== null && (typeof x == "object" && typeof x == "function")) {
        try {
            let then = x.then;
            if (typeof then == "function") {
                then.call(this, function (y) {
                    resolvePromise(p2, y, resolve, reject)
                }, function (err) {
                    reject(err)
                })
            } else {
                resolve(x)
            }
        } catch (error) {
            reject(error)
        }
    } else {
        // 说明是一个普通的值
        resolve(x)
    }
}

// 实现promise.all promise.race

// 如何实现串行并行